package com.example.springcloud.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.example.springcloud.mapper.ProductsMapper;
import com.example.springcloud.request.SubmitOrderRequest;
import com.example.springcloud.vo.OrdersVo;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springcloud.mapper.OrdersMapper;
import com.example.springcloud.domain.Orders;
import com.example.springcloud.service.IOrdersService;

/**
 * 用户订单Service业务层处理
 * 
 * @author hx
 * @date 2025-02-14
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 查询用户订单
     * 
     * @param orderId 用户订单主键
     * @return 用户订单
     */
    @Override
    public Orders selectOrdersByOrderId(Long orderId)
    {
        return ordersMapper.selectOrdersByOrderId(orderId);
    }

    /**
     * 查询用户订单列表
     * 
     * @param orders 用户订单
     * @return 用户订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增用户订单
     * 
     * @param orders 用户订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改用户订单
     * 
     * @param orders 用户订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除用户订单
     * 
     * @param orderIds 需要删除的用户订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrderIds(Long[] orderIds)
    {
        return ordersMapper.deleteOrdersByOrderIds(orderIds);
    }

    /**
     * 删除用户订单信息
     * 
     * @param orderId 用户订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrderId(Long orderId)
    {
        return ordersMapper.deleteOrdersByOrderId(orderId);
    }

    /**
     * 提交订单
     *
     * @param submitOrderRequest 提交订单请求
     * @return 结果
     */
    @Override
    public AjaxResult submitOrder(SubmitOrderRequest submitOrderRequest) {
        Orders orders = new Orders();
        Date now = new Date();
        orders.setOrderId(generateOrderId(submitOrderRequest.getUserId(), now));
        orders.setUserId(submitOrderRequest.getUserId());
        orders.setProductId(submitOrderRequest.getProductId());
        orders.setTitle(submitOrderRequest.getTitle());
        orders.setContent("订单已支付");
        orders.setCreatedAt(now);
        ordersMapper.insertOrders(orders);
        return AjaxResult.success("提交订单成功");
    }

    /**
     * 查询我的订单列表
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public List<OrdersVo> selectMyOrders(Long userId) {
        // 查询订单列表
        Orders orders = new Orders();
        orders.setUserId(userId);
        List<Orders> orderList = ordersMapper.selectOrdersList(orders);
        List<OrdersVo> ordersVoList = new ArrayList<>();
        orderList.forEach(order -> {
            OrdersVo ordersVo = new OrdersVo();
            BeanUtils.copyProperties(order, ordersVo);
            // 商品图片
            ordersVo.setProductImage(productsMapper.selectProductsByProductId(order.getProductId()).getImageUrl());
            ordersVoList.add(ordersVo);
        });

        return ordersVoList;
    }

    // 根据用户id和时间戳加上随机数生成一个订单ID
    private String generateOrderId(Long userId , Date now) {
        // 使用SimpleDateFormat格式化当前时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timestamp = sdf.format(now);
        // 生成一个随机数
        Random random = new Random();
        int randomNumber = random.nextInt(10000); // 生成0-9999之间的随机数
        // 将用户ID、时间戳和随机数拼接成订单ID
        return String.format("%d%s%04d", userId, timestamp, randomNumber);
    }
}
